package com.base.cn.platform.os.service.op.affiche.tmp;


import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.exam.paper.ExamPaper;
import com.base.cn.platform.os.entity.exam.task.ExamTaskPublishMsg;
import com.base.cn.platform.os.entity.op.affiche.OpReceiveAffiche;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.CuserProfile;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业发布消息提醒
 *
 * @author s.li
 * @date 2019/05/29
 */
@Component
public class PushTaskSendTemAffice extends SendTemAffice{
    @Override
    public Map<String, Object> initMsg(Object[] arr, WXTmpEnum tmpEnum, List<CusUser> userList, Map<BigDecimal, List> dataListMap) {
        initService(arr);

        String pushTaskVarNum1 = smsTempConfigMap.get("pushTaskVarNum1");
        String pushTaskVarNum2 = smsTempConfigMap.get("pushTaskVarNum2");
        String pushTaskRemark = smsTempConfigMap.get("pushTaskRemark");
        String pushTaskMsgType = smsTempConfigMap.get("pushTaskMsgType");

        if(StringUtils.isNotEmpty(pushTaskVarNum1)
                && StringUtils.isNotEmpty(pushTaskVarNum2)
                && StringUtils.isNotEmpty(pushTaskMsgType)){
            Map<String,Object> msg = new HashMap<>();
            boolean status = false;
            if(pushTaskMsgType.contains(MsgType.SITE.getKey())){
                List<OpReceiveAffiche> receiveAfficheList = new ArrayList<>();
                userList.forEach(user -> {
                    List<ExamTaskPublishMsg> examTaskPublishMsgList = (List<ExamTaskPublishMsg>)dataListMap.get(user.getId());
                    if(ObjectUtils.isNotEmpty(examTaskPublishMsgList)){
                        examTaskPublishMsgList.forEach(examPaper->{
                            OpReceiveAffiche affiche = new OpReceiveAffiche();
                            affiche.setMsgType(1);
                            affiche.setStatus(1);
                            affiche.setUserId(user.getId());
                            StringBuilder orderContext = new StringBuilder();
                            orderContext.append("新作业提醒，");
                            orderContext.append(pushTaskVarNum1.replace("{1}","1"));
                            orderContext.append(" , ");
                            orderContext.append(pushTaskVarNum2.replace("{2}",examPaper.getTaskName()));
                            if(StringUtils.isNotEmpty(pushTaskRemark)){
                                orderContext.append(" ， ");
                                orderContext.append(pushTaskRemark);
                            }
                            affiche.setContext(orderContext.toString());
                            receiveAfficheList.add(affiche);
                        });
                    }
                });
                msg.put(SITE_MESSAGE_KEY,receiveAfficheList);
                status = true;
            }
            if(pushTaskMsgType.contains(MsgType.WX.getKey())){
                List<CuserProfile> cuserProfileList =this.getCuserProfileList(userList);
                if(ObjectUtils.isNotEmpty(cuserProfileList)){
                    String tmpId = getTmpId(tmpEnum);
                    List<JSONObject> jsonObjectList = new ArrayList<>();
                    cuserProfileList.forEach(cuserProfile -> {
                        List<ExamTaskPublishMsg> examTaskPublishMsgList = (List<ExamTaskPublishMsg>)dataListMap.get(cuserProfile.getId());
                        if(ObjectUtils.isNotEmpty(examTaskPublishMsgList)){
                            examTaskPublishMsgList.forEach(examPaper->{
                                //设置参数
                                JSONObject obj = new JSONObject();
                                obj.put("touser", cuserProfile.getOpenId());
                                obj.put("template_id", tmpId);
                                //微信消息数据内容
                                Map<String,Map<String,String>> data = new HashMap<>();
                                //消息标题
                                Map<String,String> first = new HashMap<>();
                                first.put("value","新作业发布提醒");
                                first.put("color","#173177");
                                data.put("first",first);

                                Map<String,String> keyword1 = new HashMap<>();
                                keyword1.put("value","1");
                                keyword1.put("color","#173177");
                                data.put("keyword1",keyword1);

                                Map<String,String> keyword2 = new HashMap<>();
                                keyword2.put("value", examPaper.getTaskName());
                                keyword2.put("color","#173177");
                                data.put("keyword2",keyword2);
                                this.setRemarkMap(data,pushTaskRemark);
                                obj.put("data", data);
                                jsonObjectList.add(obj);
                            });
                        }
                    });
                    msg.put(WX_MESSAGE_KEY,jsonObjectList);
                    status = true;
                }
            }
            if(status){
                msg.put(MSG_TYPE,pushTaskMsgType);
                return msg;
            }
        }
        return null;
    }
}
